package com.diandian.pos.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;


/**
 * 链接工具类
 *
 * @author wupengfei
 *
 */
public abstract class HttpUtils {

	private static final Logger LOGGER = Logger.getLogger(HttpUtils.class);

	/**
	 * 调用get方法
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static List<String> doGet(String url) throws IOException {
		return doGet(url, "utf-8", "utf-8");
	}

	/**
	 * 调用get方法
	 * 
	 * @param url
	 * @param requestCharset
	 * @param receiveCharset
	 * @return
	 * @throws IOException
	 */
	public static List<String> doGet(String url, String requestCharset, String receiveCharset) throws IOException {
		if (StringUtils.isBlank(url)) {
			return null;
		}
		requestCharset = StringUtils.isEmpty(requestCharset) ? "utf-8" : requestCharset;
		receiveCharset = StringUtils.isEmpty(receiveCharset) ? "utf-8" : receiveCharset;

		List<String> result = null;
		HttpURLConnection connection = null;
		try {
			LOGGER.info("开始调用url=" + url);
			connection = getConnection(url, requestCharset);
			result = getContent(connection.getInputStream(), receiveCharset);
		} catch (Exception e) {
			LOGGER.error("调用url=" + url + "时发生错误", e);
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		return result;
	}

	/**
	 * 获取连接
	 * 
	 * @param url
	 * @param charset
	 * @return
	 * @throws IOException
	 */
	private static HttpURLConnection getConnection(String url, String charset) throws IOException {
		charset = StringUtils.isEmpty(charset) ? "utf-8" : charset;

		URL initUrl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) initUrl.openConnection();
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

		connection.connect();
		return connection;
	}

	/**
	 * 获取流内容，并关闭流
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static List<String> getContent(InputStream is) throws IOException {
		return getContent(is, null);
	}

	/**
	 * 获取流内容，并关闭流
	 * 
	 * @param is
	 * @param charset
	 * @return
	 * @throws IOException
	 */
	public static List<String> getContent(InputStream is, String charset) throws IOException {
		if (is == null) {
			return null;
		}
		charset = StringUtils.isEmpty(charset) ? "utf-8" : charset;

		List<String> result = new ArrayList<String>();
		BufferedReader buffer = null;
		try {
			buffer = new BufferedReader(new InputStreamReader(is, charset));
			String response;
			while ((response = buffer.readLine()) != null) {
				result.add(response);
			}
			return result;
		} finally {
			buffer.close();
		}
	}
	
	public static String toString(List<String> list) {
		StringBuilder result = new StringBuilder();
		int size = (list != null) ? list.size() : 0;
		for (int i = 0; i < size; i++) {
			result.append(list.get(i));
		}
		return result.toString();
	}

}
